package com.ycsj.goldmedalnewconcept.fragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.ycsj.goldmedalnewconcept.MainActivity;
import com.ycsj.goldmedalnewconcept.activity.ParentsEvaluationActivity;
import com.ycsj.goldmedalnewconcept.sortlistview.CharacterParser;
import com.ycsj.goldmedalnewconcept.sortlistview.ClearEditText;
import com.ycsj.goldmedalnewconcept.sortlistview.PinyinComparator;
import com.ycsj.goldmedalnewconcept.sortlistview.SortAdapter;
import com.ycsj.goldmedalnewconcept.sortlistview.SortAdapterTeachermanager;
import com.ycsj.goldmedalnewconcept.sortlistview.SortModel;
import com.ycsj.goldmedalnewconcept.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

//教师管理
public class TeacherManageFragment extends Fragment {
	
	private Activity ctx;
	private View layout;
	private MainActivity parentActivity;	
	private ListView sortListView;
//	private SideBar sideBar;
	private TextView dialog;
	private SortAdapterTeachermanager adapter;
	private ClearEditText mClearEditText;
	private PinyinComparator pinyinComparator;
	private CharacterParser characterParser;
	private List<SortModel> SourceDateList;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		if (layout == null) {
			ctx = this.getActivity();
			parentActivity = (MainActivity) getActivity();
			layout = ctx.getLayoutInflater().inflate(R.layout.fragment_teachermanage,
					null);
		} else {
			ViewGroup parent = (ViewGroup) layout.getParent();
			if (parent != null) {
				parent.removeView(layout);
			}
		}
		initViews(layout);
		return layout;
	}
	private void initViews(View view) {


		//ʵ����תƴ����
		characterParser = CharacterParser.getInstance();
		
		pinyinComparator = new PinyinComparator();
		
//		sideBar = (SideBar) view.findViewById(R.id.sidrbar);
		dialog = (TextView) view.findViewById(R.id.dialog_teachermanage);
//		sideBar.setTextView(dialog);
		
		//�����Ҳഥ������
//		sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
//			
//			@Override
//			public void onTouchingLetterChanged(String s) {
//				//����ĸ�״γ��ֵ�λ��
//				int position = adapter.getPositionForSection(s.charAt(0));
//				if(position != -1){
//					sortListView.setSelection(position);
//				}
//				
//			}
//		});
		
		sortListView = (ListView) view.findViewById(R.id.country_lvcountry_teachermanage);
		sortListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//����Ҫ����adapter.getItem(position)����ȡ��ǰposition���Ӧ�Ķ���
//				Toast.makeText(getActivity(), ((SortModel)adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show();
			Intent intent=new Intent(getActivity(), ParentsEvaluationActivity.class);
			startActivity(intent);
			}
		});
		
//		SourceDateList = filledData(getResources().getStringArray(R.array.date));
		
		// ���a-z��������Դ���
		Collections.sort(SourceDateList, pinyinComparator);
		adapter = new SortAdapterTeachermanager(getActivity(), SourceDateList);
		sortListView.setAdapter(adapter);
		
		
		mClearEditText = (ClearEditText) view.findViewById(R.id.filter_edit_teachermanage);
		
		//������������ֵ�ĸı�����������
		mClearEditText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				//������������ֵΪ�գ�����Ϊԭ�����б?����Ϊ��������б�
				filterData(s.toString());
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
			}
		});
	
		
	
		
	}

	private List<SortModel> filledData(String [] date){
		List<SortModel> mSortList = new ArrayList<SortModel>();
		
		for(int i=0; i<date.length; i++){
			SortModel sortModel = new SortModel();
			sortModel.setName(date[i]);
			//����ת����ƴ��
			String pinyin = characterParser.getSelling(date[i]);
			String sortString = pinyin.substring(0, 1).toUpperCase();
			
			// ������ʽ���ж�����ĸ�Ƿ���Ӣ����ĸ
			if(sortString.matches("[A-Z]")){
				sortModel.setSortLetters(sortString.toUpperCase());
			}else{
				sortModel.setSortLetters("#");
			}
			
			mSortList.add(sortModel);
		}
		return mSortList;
		
	}
	
	/**
	 * ���������е�ֵ��������ݲ�����ListView
	 * @param filterStr
	 */
	private void filterData(String filterStr){
		List<SortModel> filterDateList = new ArrayList<SortModel>();
		
		if(TextUtils.isEmpty(filterStr)){
			filterDateList = SourceDateList;
		}else{
			filterDateList.clear();
			for(SortModel sortModel : SourceDateList){
				String name = sortModel.getName();
				if(name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString())){
					filterDateList.add(sortModel);
				}
			}
		}
		
		// ���a-z��������
		Collections.sort(filterDateList, pinyinComparator);
		adapter.updateListView(filterDateList);
	}
}
